<?php $this->_extends('_layouts/default_layout'); ?>

<?php $this->_block('title'); ?><?= $title ?><?php $this->_endblock(); ?>
<?php $this->_block('js'); ?>
<script src="<?=$_BASE_DIR?>js/jquerytools/1.2.5/jquery.tools.min.js"></script>
<script type="text/javascript" src="<?=$_BASE_DIR?>js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
    <?
    $tinyMCE_El_Ids = 'body';
    include 'js/tiny_mce/setup_exact_items.js.php';
    ?>

    $(function() {

        var
                $addTypeBtn = $("#addTypeBtn"),
                $addTypeForm = $('#addTypeForm'),
                $addTypeResult = $('#addTypeResult'),
                $addTypeResult = $('#addTypeResult'),
                $newType = $('#newType'),
                $type_id = $('#type_id'),

                $addProfessionBtn = $("#addProfessionBtn"),
                $addProfessionForm = $('#addProfessionForm'),
                $addProfessionResult = $('#addProfessionResult'),
                $addProfessionResult = $('#addProfessionResult'),
                $newProfession = $('#newProfession'),
                $profession_id = $('#profession_id');


        $addTypeBtn.overlay({
            mask: {
                color: '#999',
                loadSpeed: 200,
                opacity: 0.9
            },
            onLoad:function() {
                $newType.focus()
            }
        });

        $addProfessionBtn.overlay({
            mask: {
                color: '#999',
                loadSpeed: 200,
                opacity: 0.9
            },
            onLoad:function() {
                $newProfession.focus()
            }
        });

        $addTypeForm.submit(function() {
            var newType = $newType.val();

            $.ajax({
                url:"<?=url('/addType')?>",
                data: 'name=' + newType,
                dataType:'json',
                type:'post',
                beforeSend: function() {
                    $addTypeResult.html('processing...');
                },
                success:function(res) {
                    if (res.ack == 'success') {
                        var newType = res.newType;
                        $addTypeBtn.overlay().close();
                        var option = '<option value="' + newType.mail_type_id + '">' + newType.name + '</option>';
                        $type_id.append(option);
                        $type_id.val(newType.mail_type_id)
                    } else {
                        alert(res.msg);
                    }
                },
                complete:function() {
                    $addTypeResult.html('');
                }
            });

            return false; //cancel the submit action
        });

        $addProfessionForm.submit(function() {
            var newProfession = $newProfession.val();

            $.ajax({
                url:"<?=url('profession/add')?>",
                data: 'name=' + newProfession,
                dataType:'json',
                type:'post',
                beforeSend: function() {
                    $addProfessionResult.html('processing...');
                },
                success:function(res) {
                    if (res.ack == 'success') {
                        var newProfession = res.newProfession;
                        $addProfessionBtn.overlay().close();
                        var option = '<option value="' + newProfession.profession_id + '">' + newProfession.name + '</option>';
                        $profession_id.append(option);
                        $profession_id.val(newProfession.profession_id)
                    } else {
                        alert(res.msg);
                    }
                },
                complete:function() {
                    $addProfessionResult.html('');
                }
            });

            return false; //cancel the submit action
        });
    });

</script>

<style type="text/css">
    .overlay {
        background-color: #fff;
        display: none;
        width: 350px;
        padding: 15px;
        text-align: left;
        border: 2px solid #333;

        opacity: 0.8;
        -moz-border-radius: 6px;
        -webkit-border-radius: 6px;
        -moz-box-shadow: 0 0 50px #ccc;
        -webkit-box-shadow: 0 0 50px #ccc;
    }

    .overlay td {
        background: #ffffff
    }

    .overlay h2 {
        padding: 10px;
        border-bottom: 1px solid #333;
        font-size: 20px;
    }
</style>
<?php $this->_endblock('js'); ?>



<?php $this->_block('actionBar'); ?>
<?php $this->_endblock('actionBar'); ?>

<?php $this->_block('contents'); ?>

<? if (isset($errorMsg)): ?>
<div class="error"><?=$errorMsg?></div>
<? endif ?>

<form id=mainForm method="post" enctype="multipart/form-data">
    <table>
        <? if ($mail->isTpl()): ?>
        <tr>
            <td align="right" width="70px" class="b">模板分类</td>
            <td><?=Q::control('dropdownlist', 'type_id', array('items' => Mail_Type::items(), 'value' => $mail->type_id, 'emptyText' => '--请选择--'))?>
                <a id="addTypeBtn" href="javascript:;" rel="#addTypePanel">增加分类</a></td>
        </tr>
        <tr>
            <td align="right" width="70px" class="b">行业分类</td>
            <td><?=Q::control('dropdownlist', 'profession_id', array('items' => Profession::items(), 'value' => $mail->profession_id, 'emptyText' => '--请选择--'))?>
                <a id="addProfessionBtn" href="javascript:;" rel="#addProfessionPanel">增加行业</a></td>
        </tr>
        <tr>
            <td align="right" width="70px" class="b">模板名称</td>
            <td width="700px"><input name="name" size="32" value="<?=$mail->name?>"></td>
        </tr>
        <? endif ?>
        <? if (!$mail->isTpl()): ?>
        <tr>
            <td align="right" width="70px" class="b">发信帐号</td>
            <td width="700px"><input name="from" style="width:100%" value="<?=$mail->from?>"></td>
        </tr>
        <tr>
            <td align="right" class="b">收件人</td>
            <td><input name="to" style="width:100%" value="<?=$mail->to?>"></td>
        </tr>
        <? endif ?>
        <? if ($mail->isTpl()): ?>
        <tr>
            <td align="right" width="70px" class="b">摘要</td>
            <td width="700px"><textarea rows="3" cols="100" name="summary"><?=$mail->summary()?></textarea></td>
        </tr>
        <? endif ?>
        <tr>
            <td align="right" class="b">主题</td>
            <td><input name="subject" style="width:100%" value="<?=$mail->subject?>"></td>
        </tr>
        <tr>
            <td align="right" class="b">附件</td>
            <td>
                <? foreach ($mail->attachments as $attach): ?>
                    <?=$attach->name;?><br>
                <? endforeach ?>
                <input name="uploadFile[]" type="file">
            </td>
        </tr>
        <tr>
            <td colspan="2"><textarea name="body" id="body"><?=$mail->body?></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="保存">
            </td>
        </tr>
    </table>
</form>

<div id="addTypePanel" class="overlay">
    <h2>增加模板分类</h2>

    <form id="addTypeForm">
        分类名称
        <input id="newType">
        <br><br>
        <input type="submit" value="确定">
        <input type="button" value="返回" class="close">
    </form>

    <div id="addTypeResult"></div>

</div>

<div id="addProfessionPanel" class="overlay">
    <h2>增加行业</h2>

    <form id="addProfessionForm">
        行业名称
        <input id="newProfession">
        <br><br>
        <input type="submit" value="确定">
        <input type="button" value="返回" class="close">
    </form>


    <div id="addProfessionResult"></div>

</div>

<?php $this->_endblock(); ?>